package com.frank.service.impl;

import com.frank.dao.JobMapper;
import com.frank.dao.RecommendMapper;
import com.frank.dto.CommonListDto;
import com.frank.dto.PushDto;
import com.frank.dto.QueryPageDto;
import com.frank.model.Job;
import com.frank.model.JobCriteria;
import com.frank.model.Recommend;
import com.frank.service.PushManageService;
import com.frank.utils.RecruitProcessEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PushManageServiceImpl implements PushManageService {

    @Resource
    private RecommendMapper recommendMapper;

    @Resource
    private JobMapper jobMapper;

    @Override
    public CommonListDto getPushList(QueryPageDto queryPageDto) {
        int count = recommendMapper.selectRecommendListCount();

        List<Map> mapList = recommendMapper.selectRecommendList(queryPageDto.getMap(),queryPageDto.getRowBounds());
        List<PushDto> pushDtoList = new ArrayList<>();
        for (Map map : mapList){
            PushDto pushDto = new PushDto();
            pushDto.setRecommendId((Integer)map.get("id"));
            pushDto.setCity((String)map.get("city"));
            pushDto.setJob_name((String)map.get("job_name"));
            pushDto.setName((String)map.get("user_name"));
            pushDto.setRecruit_type((String)map.get("recruit_type"));
            pushDto.setUser_email((String)map.get("user_email"));
            pushDto.setRemark((String)map.get("remark"));
            pushDto.setRecommend_staff((String)map.get("staff_name"));
            pushDto.setRecommend_staff_id((Integer)map.get("staff_id"));
            if (map.get("process_state") != null){
                pushDto.setProcess_state(RecruitProcessEnum.getName((Integer)map.get("process_state")));
            }else {
                pushDto.setProcess_state("暂无");
            }
            Date create_date = (Date)map.get("create_time");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            pushDto.setCreate_time(simpleDateFormat.format(create_date));

            pushDtoList.add(pushDto);
        }

        return new CommonListDto<>(pushDtoList, count);
    }

    @Override
    public boolean addPush(PushDto pushDto) {
        JobCriteria jobCriteria = new JobCriteria();
        jobCriteria.createCriteria().andJobNameEqualTo(pushDto.getJob_name());
        List<Job> jobList = jobMapper.selectByExample(jobCriteria);
        if (jobList.isEmpty()){
            return false;
        }
        int jobId = jobList.get(0).getId();

        Recommend recommend = new Recommend();
        recommend.setJobId(jobId);
        recommend.setStaffId(pushDto.getRecommend_staff_id());
        recommend.setRemark(pushDto.getRemark());
        recommend.setUserEmail(pushDto.getUser_email());
        recommend.setUserName(pushDto.getName());

        return recommendMapper.insertSelective(recommend) == 1;
    }

    @Override
    public boolean modifyPush(Recommend recommend) {
        return recommendMapper.updateByPrimaryKeySelective(recommend) == 1;
    }

    @Override
    public boolean deletePush(Integer pushId) {
        return recommendMapper.deleteByPrimaryKey(pushId) == 1;
    }
}
